layui.config({
    base: '/static/js/lib',
    version: new Date().getTime()
}).use(['layer', 'element'], function () {
    $ = layui.jquery;
    var layer = layui.layer;
    var table = $(".course-cate-table");
    var depthSpace = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    var cateHandleHtml = "<button class='layui-btn layui-btn-xs add-btn'>添加子分类</button><button class='layui-btn layui-btn-xs edit-btn'>编辑</button><button class='layui-btn layui-btn-xs del-btn'>删除</button>";
    loadCourseCate();
    // 获取所有的未删除分类
    function loadCourseCate() {
        $.get("/courseCate/all", function (res) {
            if (res.code == "00000") {
                var cates = res.data;
                for (var i = 0; i < cates.length; i++) {
                    courseCatesRender(cates[i], 0);
                }
                courseCateEvent();
            } else {
                console.log(res);
            }
        });
    }

    function courseCatesRender(cate, depth) {
        var cateName = cate.cateName;
        var indent = "";
        for (var i = 0; i < depth; i++) {
            indent = depthSpace + indent;
        }
        var isHidden = "";
        if (depth > 0) {
            var isHidden = "style='display:none;'";
        }
        var hasChilds = cate.childs.length > 0;
        cateHtml = "";
        if (hasChilds) {
            cateHtml = "<tr id='" + cate.id + "' parent-id='" + cate.parentId + "' " + isHidden + "><td><a href='javascript:void(0)'>" + indent + "<span is-open='false'><i class='layui-icon'>&#xe602;</i></span>&nbsp;&nbsp;" + cateName + "</a></td><td>" + cateHandleHtml + "</td></tr>";
        } else {
            cateHtml = "<tr id='" + cate.id + "' parent-id='" + cate.parentId + "' " + isHidden + "><td>" + indent + "" + cateName + "</td><td>" + cateHandleHtml + "</td></tr>";
        }
        table.append($(cateHtml));
        if (hasChilds) {
            for (var i = 0; i < cate.childs.length; i++) {
                courseCatesRender(cate.childs[i], depth + 1);
            }
        }
    }

    function close(trObj) {
        var cateId = trObj.attr("id");
        var childs = $(".course-cate-table tr[parent-id='" + cateId + "']");
        if (childs.length > 0) {
            var open = trObj.find("span");
            open.attr("is-open", "false");
            open.html("<i class='layui-icon'>&#xe602;</i>");
            childs.each(function () {
                close($(this));
            });
            childs.hide();
        }
    }

    function courseCateEvent() {
        parentCate = $(".course-cate-table a");
        parentCate.bind("click", function () {
            var cateId = $(this).parent().parent().attr("id");
            var open = $(this).find("span");
            if (open.attr("is-open") == "false") {
                open.attr("is-open", "true");
                open.html("<i class='layui-icon'>&#xe61a;</i>");
                $(".course-cate-table tr[parent-id='" + cateId + "']").show();
            } else {
                close($(this).parent().parent());
            }
        });

        $('.add-btn').click(function () {
            var parentId = $(this).parent().parent().attr("id");
            var title = "添加课程子分类";
            if (!parentId || parentId == "undefined") {
                parentId = 0;
                title = "添加一级分类";
            }

            var url = "/courseCate/edit?parentId=" + parentId ;
            save(title, url)
        });
        $(".edit-btn").click(function () {
            var id = $(this).parent().parent().attr("id");
            var parentId = $(this).parent().parent().attr("parent-id");
            var title = "编辑课程分类";
            var url = "/courseCate/edit?parentId=" + parentId  + "&id=" + id;
            save(title, url)
        });

        function save(title, url) {
            layer.open({
                type: 2,
                area: ['500px', '260px'],
                maxmin: true,
                moveOut: false,
                shade: 0.4,
                async: true,
                title: title,
                content: url,
                end : function(){
                    location.replace(location.href);
                }
            });
        }

        $(".del-btn").click(function () {
            var id = $(this).parent().parent().attr("id");
            layer.confirm(
                '确定要删除吗?',
                {icon: 0, title: '提示'},
                function (index) {
                    $.post("/courseCate/del", {id: id}, function (res) {
                        var msg = "删除成功";
                        var icon = 6;
                        if (res.code != "00000") {
                            msg = res.desc;
                            icon = 5;
                        }
                        location.replace(location.href);
                        layer.msg(msg, {icon: icon, time: 1500});

                    });
                }
            );
        });
    }

});